<?php
class CommentAction extends Action {
	public function addComment() {
		$credit = Model('UserCredit');
		$intReplyUid = intval ( $_POST ['toUid'] );
		$intFid = intval ( $_POST ['fid'] );
		$intToId = intval ( $_POST ['toId'] );
		$strContent = $_POST ['comment'];
		$data ['type'] = $_POST ['type'];
		$data ['appid'] = $intFid;
		$data ['name'] = getUserName ( $this->mid );
		$data ['uid'] = $this->mid;
		$data ['comment'] = $strContent;
		$data ['touid'] = $intReplyUid;
		$data ['cTime'] = time ();
		$data ['toId'] = $intToId;
		$id = M ( 'Comment' )->add ( $data );
		$table = $_POST ['mode'];
		$field = $_POST ['field'];
		$idField = $_POST ['idField'];
		$url = $_POST ['url'];
		$title = $_POST ['title'];
		$mid = $_POST ['mid'];
		$appName = $_POST ['appName'];
		$arr = array('blog','group_wiki','group_file');
		//$targetId = M('Comment')->getLastInsID();
		if ($id) {
			if(in_array($data['type'],$arr)){
				$credit->set($this->mid,'group_comment_add');
			}
			$map [$idField] = $intFid;
			$CountMap ['appid'] = $intFid;
			$CountMap ['status'] = 0;
			$CountMap ['type'] = $_POST ['type'];
			$save [$field] = M ( 'Comment' )->where ( $CountMap )->count ();
			D ( $table )->where ( $map )->save ( $save );

			//识别url [杨德升添加]
			$strContent = preg_replace('/((?:https?|mailto).*?)(\s|　|&nbsp;|<br|\'|\"|$)/', '<a href="\1" target="_blank">\1</a>\2', $strContent);
			
			echo nl2br ( $strContent );
		} else {
			echo 0;
		}
		

		
		//发送动态和通知
		$body_data ['url'] = $url . "#comment-" . $id;
		
		$body_data ['content'] = $this->left ( t ( $strContent ), 100 );
		
		$title_data ['user'] = sprintf ( "<a href=%s>%s</a>", U ( 'space/view/index', 'uid=' . $mid ), getUserName ( $mid ) );
		$title_data ['title'] = sprintf ( "<a href=%s>%s</a>", $body_data ['url'], $title );
		$title_data ['appName'] = $appName;
		//添加2010-09-26
		$title_da['title']  = sprintf ( "<a href=%s>%s</a>", $url, $title );
		$gid = M('group_blog')->where('blogId='.$intFid)->find();
		$fid = $gid['gid'];
	
		if(isset($_POST['isGroup']) || $_POST['isGroup']==true){
			$result = service('Feed')->put('group_reply',$title_da,$body_data,$this->mid,'',$fid,1,$id);
		}

		//自保护代码：自己给自己回的。。不算
		if ($this->mid == $intReplyUid)
			return;
			
		//是否有回复对象
		if ($intReplyUid != 0) {
			$type = "comment_comment";
			if ($this->mid == $mid) {
				//我是这篇日志的所有者
				$notifyUid = $intReplyUid;
			} else {
				//我不是日志的所有者
				//目标不日志的所有者
				if ($intReplyUid != $mid) {
					$notifyUid = $mid . "," . $intReplyUid;
				} else {
					//目标是日志的所有者
					$notifyUid = $mid;
				}
				$result = service ( 'Feed' )->put ( strtolower ( $_POST ['type'] ) . "_comment", '',$title_data, $body_data );
			
			}
		} else {
			$type = strtolower ( $_POST ['type'] ) . "_comment";
			//自保护代码：自己是日志的所有者就不发通知也不发动态了;
			if ($this->mid == $mid)
				return;
			$notifyUid = $mid;
			$result = service ( 'Feed' )->put ( strtolower ( $_POST ['type'] ) . "_comment",'', $title_data, $body_data );
		}
		
		$result = service ( 'Notify' )->put ( $type, $notifyUid, $title_data, $body_data );
	
	}
	
	public function left($pString, $pLength) {
		return msubstr ( $pString, 0, $pLength );
	}
	
	public function deleteComment() {
		$table = $_POST ['mode'];
		$field = $_POST ['field'];
		$idField = $_POST ['idField'];
		$id = intval ( $_POST ['id'] );
		$intFid = intval ( $_POST ['fid'] );
		
		$map ['id'] = $id;
		$save ['status'] = 1;
		$result = M ( 'Comment' )->where ( $map )->save ( $save );
		
		$type = M ( 'Comment' )->where ( $map )->field ( 'type' )->find ();
		$type = $type ['type'];
		
		if ($result) {
			$map2 [$idField] = $intFid;
			
			$countMap ['status'] = 0;
			$countMap ['appid'] = $intFid;
			$countMap ['type'] = $type;
			
			unset($save);
			$save [$field] = M ( 'Comment' )->where ( $countMap )->count ();
			D ( $table )->where ( $map2 )->save ( $save );
			echo 1;
		} else {
			echo 0;
		}
	}
}
?>